package dsmflex.taskengine;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.liusy.taskengine.JobListTask;
import com.liusy.taskengine.JobTask;
import com.liusy.taskengine.TaskEngineConstant;
import com.liusy.taskengine.resultmodel.ResultModel;

/**
 * 任务引擎（门面）
 * 
 * @author liusy 2012-12-11
 */
public class DsmflexStartTaskEngineMain {
	
	private static Logger log = Logger.getLogger(DsmflexStartTaskEngineMain.class);
	
	private JobTask jt = null;
	
	
	
	public String sql = "select gc.groupdetailname as name, "+
       "gc.consumerid as consumerId, "+
       " gc.consumername as consumerName, "+
       "gc.zoneid as zoneId, "+
       "gc.linename as lineName, "+
       " gc.lineId as lineId, "+
       "gc.substationname as substationName, "+
       "gc.substationid as substationId, "+
       "gc.transformername as transformername, "+
       "gc.transformerid as transformerid, "+
       "ecs.max_load as maxLoad, "+
       "ecs.RETAIN_LOAD  as retainLoad, "+
       "case when gc.isimportant = '1' then "+
       "'是'  "+
       "else  "+
       "'否' "+
       "end as isImportant "+
       "from dsm_peak_group_customer        gc, "+
       "dsm_peak_customer_group_detail d, "+
       " dsm_peak_programme             p, "+
       " dsm_peak_sub_programme         sp, "+
       "dsm_peak_tasks                 t, "+
       "ex_customer_sub                ecs "+
       " where gc.groupdetailid = d.id "+
       " and d.customergroupid = p.customergroupid "+
       " and p.id = sp.peakprogrammeid "+
       " and  ecs.consumer_id(+) = gc.consumerid "+
       " and d.enabled = '1' "+
       " and p.enabled = '1' "+
       " and sp.enabled = '1' "+
       " and t.enabled = '1' "+
		   " and t.subplanid = sp.id "+
		   "  and t.id = "+
      " (select max(pt.id) from dsm_peak_tasks pt where pt.enabled = '1') "+
  " and (select case "+
                " when to_char(dpt.starttime, 'D') = '2' then "+
                 " wa.week1 "+
                 "when to_char(dpt.starttime, 'D') = '3' then "+
                "  wa.week2 "+
               "  when to_char(dpt.starttime, 'D') = '4' then "+
               "   wa.week3 "+
               "  when to_char(dpt.starttime, 'D') = '5' then "+
                "  wa.week4 "+
               "  when to_char(dpt.starttime, 'D') = '6' then "+
                "  wa.week5 "+
               "  when to_char(dpt.starttime, 'D') = '7' then "+
                "  wa.week6 "+
                " when to_char(dpt.starttime, 'D') = '1' then "+
                 " wa.week7 "+
              " end as week "+
         " from dsm_peak_week_arrangement wa, dsm_peak_tasks dpt "+
        " where dpt.subplanid = wa.subprogrammeid "+
          " and dpt.id = (select max(pt.id) "+
                          " from dsm_peak_tasks pt "+
                        "  where pt.enabled = '1') "+
          " and wa.enabled = '1') like "+
      " '%' || substr(gc.groupdetailname, 1, 1) || '%' order by d.name";
	
	public DsmflexStartTaskEngineMain(){
	}
	
	//开始运行
	public void startRunTask() {
		try {
			
		List<Map<String,Object>> mapTask=new ArrayList<Map<String,Object>>();
		File f = new File("C:\\Users\\Administrator\\Desktop\\1.txt");
		InputStream is = new FileInputStream(f);
		BufferedReader br = new BufferedReader(new InputStreamReader(is,"gbk"));
		String line =  br.readLine();
		while(line!= null){ 
			System.out.println(line);   
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("name", line );
			mapTask.add(map);
			line = br.readLine();
		} 
		
		JobListTask jst = new JobListTask(mapTask,SqlTaskModel.class.getName(),new ResultModel(TaskEngineConstant.COMPARISON_TASK_CONNECTION,""));
		
		jst.StartReleaseTask();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		IJobModel sjm = new DBJobModel(sql,1000,SqlExecuteModel.class.getName(),SqlTaskModel.class.getName());
//		
//		jt = new JobTask(sjm);
//	//	log.info("从任务池中取出的任务数： "+sjm.getTaskPoolCount());
//		
//		jt.AddReleaseTaskListener(ReleaseTask.CACHE_TASK_FINISH_LISTENER, new SqlCacheTaskFinishListener());
//		jt.start();
		
	}
	
	
	//停止运行
	public void stopRunTask() {
		// TODO Auto-generated method stub
		jt.interrupt();
	}
	//暂停运行
	public void suspendRunTask() {
		jt.Suspend();

	}
	//恢复运行
	public void reRunTask() {
		jt.reRun();
 
	}
	
	public static void main(String[] args) {
		
		DsmflexStartTaskEngineMain stem  = new DsmflexStartTaskEngineMain();
		stem.startRunTask();

	}
	
	

	
	
}
